package cqrtplm.aop;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hustcad.plm.pdm.caddoc.model.dto.ImportAttributeDTO;
import com.hustcad.plm.pdm.partbom.dto.partimport.PartXmlAllNodeDTO;
import com.hustcad.plm.pdm.partbom.dto.partimport.PartXmlNodeDTO;
import com.hustcad.plm.pdm.partbom.dto.partimport.PartXmlStructNodeDTO;
import cqrtplm.dto.query.RtSegmNumQueryDTO;
import cqrtplm.entity.RtSegmNumDO;
import cqrtplm.service.RtSegmNumService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Aspect
@Component
@Slf4j
public class HandleBeforeCreatePartBomAOP {
    @Resource
    private RtSegmNumService rtSegmNumService;

    /**
     * 导入BOM属性校验
     */
    @Before(value = "execution(* com.hustcad.plm.pdm.extension.service.cadimport.TyplmImportExtensionServie.handleBeforeCreatePartBom(..))")
    public void handleBeforeCreatePartBom(JoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if (ObjectUtil.isNotEmpty(args[1])) {
            PartXmlAllNodeDTO partXmlAllNodeDTO = (PartXmlAllNodeDTO) args[1];
            try {
                List<RtSegmNumDO> segmNumList = rtSegmNumService.listAll(new RtSegmNumQueryDTO());
                List<PartXmlNodeDTO> nodeDTOPartList = partXmlAllNodeDTO.getPartList();
                List<PartXmlStructNodeDTO> structNodeDTOList = partXmlAllNodeDTO.getPartStruct();
                List<String> errorList = new ArrayList<>();
                if (CollUtil.isNotEmpty(structNodeDTOList)) {
                    for (PartXmlStructNodeDTO partXmlStructNodeDTO : structNodeDTOList) {
//                        String childId = partXmlStructNodeDTO.getChildId();
//                        String parentId = partXmlStructNodeDTO.getParentId();
//                        List<PartXmlNodeDTO> child = nodeDTOPartList.stream().filter(item -> item.getId().equals(childId)).collect(Collectors.toList());
//                        List<PartXmlNodeDTO> parent = nodeDTOPartList.stream().filter(item -> item.getId().equals(parentId)).collect(Collectors.toList());
                        List<ImportAttributeDTO> attributes = partXmlStructNodeDTO.getAttributes();
                        if (CollUtil.isNotEmpty(attributes)) {
                            List<ImportAttributeDTO> segmNums = attributes.stream().filter(item -> item.getAttributeKey().contains("SegmNum")).collect(Collectors.toList());
                            if (CollUtil.isNotEmpty(segmNums)) {
                                String segmNum = segmNums.get(0).getAttributeValue();
                                if (StringUtils.isNotBlank(segmNum)) {
                                    if (segmNumList.stream().noneMatch(item -> item.getId().equals(segmNum))) {
                                       if (!errorList.contains(segmNum)){
                                           errorList.add(segmNum);
                                       }
                                    }
                                }
                            }
                        }
                    }
                }
                if (CollUtil.isNotEmpty(errorList)) {
                    throw new RuntimeException("提示：段号" + String.join(",", errorList) + "不存在，请检查！");
                }
            } catch (Exception e) {
                throw e;
            }
        }
    }
}